﻿using System;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Model;

namespace Web.Areas.Manager.Controllers
{
    [NoCache]
    [HandleError]
    [UserAuthorize]
    public class Information_ChannelController : Controller
    {
        Information_Channel_BLL _Information_Channel_BLL = new Information_Channel_BLL();

        public virtual ActionResult Index(int? pageIndex, string keyword)
        {
            var model = _Information_Channel_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.DisplayName.Contains(keyword) || a.Abbreviate.Contains(keyword));
            }
            return View(model.ToPagedList(pageIndex));
        }

        public virtual ActionResult Details(Guid id)
        {
            var item = _Information_Channel_BLL.Get(id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? id, string direction)
        {
            //更新顺序
            if (!string.IsNullOrEmpty(direction) && id != null)
            {
                _Information_Channel_BLL.ChangeOrder(id.Value, direction);
                return RedirectToAction("Index", RouteData.Values);
            }

            //模板
            Template_BLL _Template_BLL = new Template_BLL();
            ViewData["Template"] = _Template_BLL.Get();

            //显示现有
            ViewData["Information_Channel"] = _Information_Channel_BLL.Get();

            var item = _Information_Channel_BLL.Get(id);
            return View(item);
        }

        [HttpPost]
        public virtual ActionResult Edit(Guid? id, Information_Channel collection, Guid treeid)
        {
            Edit(id,  null);

            if (!ModelState.IsValid)
            {
                return View();
            }

            _Information_Channel_BLL.Save(id, ref collection, treeid);
            return RedirectToAction("Index", RouteData.Values);
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            _Information_Channel_BLL.DeleteObject(Id);
            return RedirectToAction("Index", RouteData.Values);
        }
    }
}
